import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
// 引入多个模块的规则
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'
import userRouter from './modules/user'



import Layout from '@/layout'

// 动态路由
export const asyncRoutes = [
  approvalsRouter,
  departmentsRouter,
  employeesRouter,
  permissionRouter,
  attendancesRouter,
  salarysRouter,
  settingRouter,
  socialRouter,
  userRouter
]

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('../views/Login.vue'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  {
    path: '/import',
    component: Layout,
    hidden: true, // 隐藏在左侧菜单
    children: [{
      path: '',
      component: () => import('@/views/import')
    }]
  },
]
const router = createRouter({
  history: createWebHashHistory(), // hash模式：createWebHashHistory，history模式：createWebHistory
  routes: [...constantRoutes]
})


let menus = JSON.parse(localStorage.getItem("menus")) || []
console.log(9999, menus);

const newRoutes = [] // 存储当前用户所拥有的权限路由
menus.forEach(key => {
  // key标识
  // 洒
  newRoutes.push(...asyncRoutes.filter(item => item.name === key))
})
newRoutes.forEach(item => {
  console.log(item, 32);
  router.addRoute(item)
});

// const routes = [
//   // 登陆
//   {
//     path: '/login',
//     name: 'Login',
//     component: () => import('../views/Login.vue'),
//   },
//   //以下为测试页面
//   {
//     path: '/about', 
//     name: 'About',
//     component: () => import('../views/About.vue')
//   },
//    //根目录,到登录页面
//    {
//     path: '/',
//     redirect: '/login'
//   },
// ]



export default router
